#include "draw.h"
#include "terminal.h"
#include "utils.h"
/* *
   * unicode box
   * https://www.runoob.com/charsets/ref-utf-box.html 
*/

namespace dw {

// 
const std::u32string style1 = U" ┌┐└┘│─";
const std::u32string style2 = U" ┏┓┗┛┃━";
const std::u32string style3 = U" ╔╗╚╝║═";
const std::u32string style4 = U" ╭╮╰╯│─";
const std::u32string cur_style = style4;

void window(int top, int left, int width, int height, std::string title) {
    
    for (int r = 0; r < height; r++) {
        tc::move_to(top + r, ut::block2col(left));
        for (int c = 0; c < width; c++) {
            if (r == 0 ) { //第一行
                if (c == 0) { // 第一列
                    std::cout << ut::utf32_to_utf8({cur_style[0], cur_style[1]});
                } else if (c == width - 1) { // 最后一列
                    std::cout << ut::utf32_to_utf8({cur_style[2]});
                } else {
                    std::cout << ut::utf32_to_utf8({cur_style[6], cur_style[6]});
                }
            } else if (r == height - 1) { // 最后一行
                if (c == 0) { // 第一列
                    std::cout << ut::utf32_to_utf8({cur_style[0], cur_style[3]});
                } else if (c == width - 1) { // 最后一列
                    std::cout << ut::utf32_to_utf8({cur_style[4]});
                } else {
                    std::cout << ut::utf32_to_utf8({cur_style[6], cur_style[6]});
                }
            } else {
                if (c == 0) { // 第一列
                    std::cout << ut::utf32_to_utf8({cur_style[0], cur_style[5]});
                } else if (c == width - 1) { // 最后一列
                    std::cout << ut::utf32_to_utf8({cur_style[5]});
                } else {
                    std::cout << "  ";
                }
            }   
        }
    }
    tc::move_to(top, ut::block2col(left) + (width*2-title.length())/2);
    std::cout << title;
}
void tetromion(gm::Tetromino_1 &t, int top, int left) {
    for (int i = 0; i < t.size(); ++i) {
        tc::move_to(top + i, ut::block2col(left));

        for (int j = 0; j < t[0].size(); ++j) {
            if (t[i][j] > 0) {
                tc::reset_color();
                tc::set_back_color(static_cast<int>(gm::tetro_color[t[i][j]]));
                std::cout << "  ";

            } else {
                tc::reset_color();
                std::cout << "  ";
            }
        }
    }
}
void tetromion(gm::Tetromino_2 &t, int top, int left, int index)
{
    for (int i = 0; i < 4; ++i) {
        tc::move_to(top + i, ut::block2col(left));

        for (int j = 0; j < 4; ++j) {
            if (gm::get_bit(t[index], i , j)) {
                tc::reset_color();
                tc::set_back_color(t[index]>>16 & 0xff);
                std::cout << "  ";
            } else {
                tc::reset_color();
                std::cout << "  ";
            }
        }
    }

}
void tetromion(gm::Tetromino_3 &t, int top, int left, int index)
{
    tc::move_to(top, ut::block2col(left));
    tc::set_back_color(t[index][0].second);
    std::cout << "  ";
    for (auto p:t[index]) {
        if (p.first > 'A') continue;

        //(dx, dy) ----> (row, col)
        //row = row - dy
        //col = col + dx
        tc::move_to(top - p.second, ut::block2col(left + p.first));
        std::cout << "  ";

    }
}
} // namespace dw